#!/usr/bin/env python
# coding=utf-8

import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import urllib2
import ConfigParser
from Route import Route
import MySQLdb

curpath = sys.path[0] + os.sep
rootpath = curpath[:curpath.find("resources")]
webroot = rootpath + "WebRoot" + os.sep
srcroot = rootpath + "src" + os.sep
config=ConfigParser.ConfigParser()
with open(srcroot + "application.properties",'r') as file:
    config.readfp(file)
    database_vacationdb=config.get('python','database_vacationdb')
    username_vacation=config.get('python','username_vacation')
    server_vacation=config.get('python','server_vacation')
    password_vacation=config.get('python','password_vacation')

tourplane_re = re.compile(r"第\s*(\d*)\s*天[^>]*>[\S\s]*?>([^<]*)</p>", re.X|re.S)

geturl_sql_temp = "SELECT id,route_url FROM route WHERE supplier_id = 1 AND id >= %s ORDER BY id ASC LIMIT 0,2" 
update_sql_temp = "UPDATE route SET destination='%s' WHERE id=%s"

def do_crawl():
     conn=MySQLdb.Connection(server_vacation,username_vacation,password_vacation,database_vacationdb,use_unicode=False,charset='utf8')
     curs=conn.cursor()
     curs.execute(geturl_sql_temp % "1")
     url_list = curs.fetchall()
     while len(url_list) >= 2:
         dest = getDestination(url_list[0][1])
         
         if dest:
             curs.execute(update_sql_temp % (dest,url_list[0][0]))
             print >>sys.stderr, 'update ', url_list[0][0],url_list[0][1]
         else:
             print >>sys.stderr, 'not updated ', url_list[0][0],url_list[0][1]
             
         curs.execute(geturl_sql_temp % url_list[1][0])
         url_list = curs.fetchall()
     print "That's all ..."

def getDestination(url):
    
    result=''
    try:
        html = urllib2.urlopen(url).read()
        tour_list = tourplane_re.findall(html)
        for tour in tour_list:
            if(result.find(tour[1])<0):
                result += tour[1]+','
        if len(result)>0:
            result=result[0:len(result)-1].replace(' ','')
    except:
        print traceback.format_exc(sys.exc_info())
        
    return result



if __name__ == '__main__':
    script_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_path)
    do_debug = False

    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
    except getopt.GetoptError:
        usage()
        sys.exit()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit()
        elif opt in ('-d', '--debug'):
            do_debug = True
        elif opt in ('-n', '--no-cache'):
            use_cache = False
        elif opt in ('-o', '--only-cache'):
            if arg.lower() in ('no', 'n', '0'):
                only_cache = False
            else:
                only_cache = True
        elif opt in ('-p', '--provider'):
            pass

    if do_debug:
        import pdb
        pdb.set_trace()
    do_crawl()